package com.sanron.sunweather.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.content.res.AssetManager;
import android.text.format.Time;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.sanron.sunweather.entity.City;

public class CommonUtils {
	
	public static List<City> cities;
	
	public static City findCityByName(String name){
		if(cities==null){
			return null;
		}
		for(City city:cities){
			if(name.equals(city.getName())){
				return city;
			}
		}
		return null;
	}
	
	/**
	 * 匹配城市
	 * @param text
	 * @return
	 */
	public static List<City> findCityMatchText(String text){
		if(cities==null){
			return null;
		}
		List<City> result = new LinkedList<City>();
		for(City city:cities){
			if(city.getName().contains(text)){
				result.add(city);
			}
		}
		return result;
	}
	
	public static void loadCities(Context context){
		
		if(cities!=null) return;
		
		cities = new LinkedList<City>();
		AssetManager assetManager = context.getAssets();
		InputStream is = null;
		InputStreamReader isr = null;
		try {
			is = assetManager.open("cities.dat");
			isr = new InputStreamReader(is);
			JsonElement element = new JsonParser().parse(isr);
			JsonArray jsonArrayP = element.getAsJsonArray();
			for(int i=0; i<jsonArrayP.size(); i++){
				//遍历省
				JsonObject jsonObjectP = jsonArrayP.get(i).getAsJsonObject();
				String idP = jsonObjectP.get("id").getAsString();
				String nameP = jsonObjectP.get("name").getAsString();
				City cityP = new City();
				cityP.setId(idP);
				cityP.setName(nameP);
				JsonElement e = jsonObjectP.get("cities");
				if(e==null){
					cities.add(cityP);
				}else{
					JsonArray jsonArrayC = e.getAsJsonArray();
					for(int ii=0; ii<jsonArrayC.size(); ii++){
						//遍历市
						JsonObject jsonObjectC = jsonArrayC.get(ii).getAsJsonObject();
						String idC = jsonObjectC.get("id").getAsString();
						String nameC = jsonObjectC.get("name").getAsString();
						City cityC = new City();
						cityC.setId(idC);
						cityC.setName(nameC);
						cityC.setParent(cityP);
						JsonElement ee = jsonObjectC.get("cities");
						if(ee==null){
							cities.add(cityC);
						}else{
							JsonArray jsonArrayA = ee.getAsJsonArray();
							for(int iii=0; iii<jsonArrayA.size(); iii++){
								//遍历县
								JsonObject jsonObjectA = jsonArrayA.get(iii).getAsJsonObject();
								String idA = jsonObjectA.get("id").getAsString();
								String nameA = jsonObjectA.get("name").getAsString();
								if(nameA.equals(cityC.getName())){
									cities.add(cityC);
									continue;
								}
								City cityA = new City();
								cityA.setId(idA);
								cityA.setName(nameA);
								cityA.setParent(cityC);
								cities.add(cityA);
							}
						}
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(isr!=null){
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
